package com.example.spinning.controller;

import com.example.spinning.bean.Organ;
import com.example.spinning.bean.RespBean;
import com.example.spinning.bean.User;
import com.example.spinning.service.UserService;
import com.example.spinning.service.UsersService;
import com.example.spinning.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */
@RestController
@RequestMapping("/user")
public class UsersController {
    @Autowired
    UsersService usersService;

    // 通过nickname查询用户与角色信息
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public List<User> getUserByNickname(String nickname) {
        return usersService.getUserByNickname(nickname);
    }

//查询用户信息
    @RequestMapping(value = "/getUsersByCondition", method = RequestMethod.GET)
    public Map<String,Object> getSpecifics( @RequestParam(value = "page") Integer page,
                                    @RequestParam(value = "count") Integer count,
                                    User user) {
        int totalCount = usersService.getUserCountByCondition(user);
        List<User> user_list = usersService.getSpecificUsers(page,count,user);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("user",user_list);
        return map;

    }

//查询全部用户
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<User> getAll() {
        return usersService.getAllUsers();
    }

    //删除用户
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteById(@PathVariable String ids) {
        boolean result = usersService.deleteUsersByIds(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }


//修改用户信息
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateUser(User user) {
        int i = usersService.updateUserById(user);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }



//    @RequestMapping("/currentUserName")
//    public String currentUserName() {
//        return Util.getCurrentUser().getNickname();
//    }
//
//    @RequestMapping("/currentUserId")
//    public Long currentUserId() {
//        return Util.getCurrentUser().getId();
//    }
//
//    @RequestMapping("/currentUserEmail")
//    public String currentUserEmail() {
//        return Util.getCurrentUser().getEmail();
//    }
//
//    @RequestMapping("/isAdmin")
//    public Boolean isAdmin() {
//        List<GrantedAuthority> authorities = Util.getCurrentUser().getAuthorities();
//        for (GrantedAuthority authority : authorities) {
//            if (authority.getAuthority().contains("超级管理员")) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    @RequestMapping(value = "/updateUserEmail",method = RequestMethod.PUT)
//    public RespBean updateUserEmail(String email) {
//        if (userService.updateUserEmail(email) == 1) {
//            return new RespBean("success", "开启成功!");
//        }
//        return new RespBean("error", "开启失败!");
//    }
}
